<template>
  <el-dialog :title="title"  :visible="visible" :close-on-click-modal="false"
             :before-close="handleClose" width="500px">
    <el-form :model="formData" :rules="rules" ref="formData" label-width="80px">
      <el-form-item label="讨论区板" prop="discussType" >
        <el-select v-model="formData.discussType" style="width:380px" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="discussTitle">
        <el-input v-model="formData.discussTitle"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="discussContent">
        <el-input v-model="formData.discussContent" 
        type="textarea"
        placeholder="请输入内容"
        maxlength="150"
        show-word-limit>
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="text" @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit"
                 :loading="loading">{{loading?'提交中...':'确认提交'}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  //import {getAllRoleList} from "../../../api/modules/role";
  import {add} from "@/api/modules/forum";
  import {getCookie} from '@/libs/cookie'
  
  export default {
    name: "edit",
    props: {
      visible: {
        type: Boolean
      },
      title: {
        type: String,
        default: '新增'
      },

      defaultFormData: {
        type: Object
      },
    },
    data() {
      return {
        options:[{
              value: '老师答疑区',
              label: '老师答疑区',
            },
            {
              value: '课堂交流区',
              label: '课堂交流区',
             
            },
            {
              value: '综合讨论区',
              label: '综合讨论区',  
            }],

        loading: false,
        username:'',
        formData: {
          discussType: '',
          discussTitle: '',
          discussContent:'',
          nickname:'',
          courseId:''
        },
        address:[],
        rules: {
          title: [
            {required: true, message: "楼栋名不能为空", trigger: "blur"}
          ],
          admin: [
            {required: true, message: "管理员不能为空", trigger: "blur"}
          ]
        },
        
        refresh: false
      }
    },
    mounted() {


      let user = JSON.parse(getCookie("user") || '{}');
      this.formData.nickname = user.nickname
       let courseID = JSON.parse(getCookie("courseID") || '{}');
        this.formData.courseId = courseID
        //console.log(this.formData)
    },
    methods: {
      
      handleChange(value) {
        console.log(value);
      },
      handleClose() {
        this.$emit('close', this.refresh)
      },
      handleSubmit() {
        this.$refs.formData.validate(valid => {
          if (valid) {
            this.loading = true
              add({
                ...this.formData,
              }).then(res => {
                this.loading = false
                if (res.success) {
                  this.refresh = true
                  this.toast(res.message)
                  this.handleClose()
                }
              })
           
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
